<?php
class Core_Business_User extends Core_Business_Abstract{
	/**
	 * private fields
	 */
	private $_id;
	private $_email;
	private $_salutation;
	private $_firstname;
	private $_lastname;
	private $_rsocial;
	private $_address1;
	private $_address2;
	private $_zipcode;
	private $_city;
	private $_created;
	private $_updated;
	private $_validated;
	private $_password;
	private $_payment;
	private $_disabled;
	private $_linkviadeo;
	private $_linklinkedin;
	private $_validationtoken;

	public function __construct() {
		$this->_validated = 0;
		$this->_disabled=0;	
		$this->_password="";
	}
	
	/**
	 * public properties
	 */
	public function getId() {return $this->_id;}
	public function setId($value) {	$this->_id = $value; }
	
	public function getEmail() { return $this->_email;}
	public function setEmail($value) {
		if(preg_match("`^[[:alnum:]]([-_.]?[[:alnum:]_?])*@[[:alnum:]]([-.]?[[:alnum:]])+\.([a-z]{2,6})$`", $value))
			$this->_email = $value;
		else
			throw new Exception("Unvalid Propertie value Email!!");
	}
	
	public function getSalutation() {return $this->_salutation;}
	public function setSalutation($value) {
		if(preg_match("`^(M|Mme|Mlle)$`", $value)) 
			$this->_salutation = $value;
		else 
			throw new Exception("Unvalid Propertie value Salutation !!");	
	}
	
	public function getFirstname() {return $this->_firstname;}
	public function setFirstname($value) {	$this->_firstname = $value;	}
	
	public function getLastname() {return $this->_lastname;}
	public function setLastname($value) {$this->_lastname = $value;	}
	
	public function getRsocial() {return $this->_rsocial;}
	public function setRsocial($value) {$this->_rsocial = $value;}
	
	public function getAddress1() {return $this->_address1;}
	public function setAddress1($value) {$this->_address1 = $value;}
	
	public function getAddress2() {return $this->_address2;}
	public function setAddress2($value) {$this->_address2 = $value;}
	
	public function getZipcode() {return $this->_zipcode;}
	public function setZipcode($value) {$this->_zipcode = $value;}
	
	public function getCity() {return $this->_city;}
	public function setCity($value) {$this->_city = $value;	}
	
	public function setCreated($value){$this->_created = $value;}
	public function getCreated() {return $this->_created;}
	
	public function setValidated($value) {$this->_validated = $value;}
	public function getValidated() {return $this->_validated;}
	
	public function setUpdated($value) {$this->_updated = $value;}
	public function getUpdated() {return $this->_updated;}
	
	public function setPassword($value) {$this->_password = sha1($value);}
	public function getPassword() {return $this->_password;}
	
	public function setPayment($value) {$this->_payment=$value;}
	public function getPayment() {return $this->_payment;}
	
	public function getLinkviadeo() {return $this->_linkviadeo;}
	public function setLinkviadeo($value) {$this->_linkviadeo = $value;}
	
	public function getLinklinkedin(){return $this->_linklinkedin;}
	public function setLinklinkedin($value){$this->_linklinkedin=$value;}
	
	public function getDisabled() {return $this->_disabled;}
	public function setDisabled($value) {$this->_disabled = $value;}
	public function isDisabled() {return $this->_disabled ==1 ? true : false;}
	
	
	public function checkPwd($value){
		$map = new Core_Data_Mappers_User();
		$query = $map->getDbTable()->Select()
			   ->where("password = SHA1(?)",$value)
			   ->where("email = ?",$this->_email);
		$resl = $map->fetchQuery($query);
		if(isset($resl) && current($resl) instanceof Core_Business_User)
			return true;
			
		return false;
	}
	
	public function getRole() {
		
		$map = new Core_Data_Mappers_Userrule();
		$rule = $map->find($this->_id);
		$rule = $rule->getRole();
		return $rule ===null ? "guest":$rule->Name;
	}
	public function getRoleId() {
		
		$map = new Core_Data_Mappers_Userrule();
		$rule = $map->find($this->_id);
		$rule = $rule->getRole();
		return $rule ===null ? "guest":$rule->Id;
	}
	
	public function save() {
		$mapper = new Core_Data_Mappers_User();
		//$mapper->add($this);
		$mapper->save($this);
		return $this;
	}
	
	public function genToken() {
		$this->_validationtoken = sha1($this->_email.$this->_lastname);
	}
	public function getToken() {return $this->_validationtoken;}
	public function setToken($value) {$this->_validationtoken=$value;}
	
	public static function getByToken($token) {
		$map = new Core_Data_Mappers_User();
		$user=$map->fetchAll(array('token = ?'=>$token));
		return current($user);
	}
	
	public static function getById($id) {
		$map = new Core_Data_Mappers_User();
		$user = $map->find($id);
		return $user;
		
	}
	
	public static function getAllUser() {
		$map = new Core_Data_Mappers_User();
		$Collection = $map->fetchAll(array());
		return $Collection;
	}
	
	public static function getByEmail($email) {
		if(isset($email)) {
			$map = new Core_Data_Mappers_User();
			//$query = new Zend_Db_Select($map->getDbTable()->getAdapter());
			//$query->from("users")
			//	  ->where(array("email = ?"=>$email));
			$user = $map->fetchAll(array("email = ?"=>$email));
			if($user instanceof Core_Business_User)
				return $user;
			else 
				return null;
		}
			else return null;
		
	} 
}
?>